package com.example.qqlaobing.Umoney.view.activity;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.CookieSyncManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.qqlaobing.Umoney.R;
import com.githang.statusbar.StatusBarCompat;

public class MyWebView extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.act_mywebview);
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#eb752f"));
        initView();
        initData();
    }

    private WebView mWebView;
    private String url;

    public void initView() {
        findViewById(R.id.tv_exit).setOnClickListener(view -> finish());
        url = getIntent().getStringExtra("url");
        mWebView = findViewById(R.id.myWebview);
        mWebView.loadUrl(url);
//不现实水平滚动条
        mWebView.setHorizontalScrollBarEnabled(false);
//不现实垂直滚动条
        mWebView.setVerticalScrollBarEnabled(false);
//滚动条在WebView内侧显示
        mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
//滚动条在WebView外侧显示
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
//获取触摸焦点
        mWebView.requestFocusFromTouch();
        mWebView.clearCache(true);
//声明WebSettings子类
        WebSettings webSettings = mWebView.getSettings();
//如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.setJavaScriptEnabled(true);
//设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
//其他细节操作
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    public void initData() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                mWebView.goBack();//后退
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
